from core import myapp
from bson.dbref import DBRef
from db.mongodb import DBConn

import time
app = myapp


class TestData():
    conn = DBConn()
    db = conn.get_database('shawe')
    
    # The following are embedded datas, the embedded datas will be created when run tests
    user_dict = {'username':'zlk', 
                       'password':'zlk',
                       'email':'zlk@forlocal.com', 
                       'role':'1', #0 user, 1 admin
                       'phone': '519-334-6720',
                       'description':'admin @ forlocal.com'
    }
    
    # user is a document object
    user = db.find_one('users', user_dict)
    user_ref = None
    if user is None:
        user_dict['created'] = time.time()
        user_id = db.save('users', user_dict)
        user_ref = DBRef('users', user_id)
    else:
        user_ref = DBRef('users', user['_id'])
    
    
    subject_dict = {'body': 'This is an embedded test subject'}
    # subject is a document object
    subject = db.find_one('subjects', subject_dict)
    subject_ref = None
    if subject is None:
        subject_id = db.save('subjects', subject_dict)
        subject_ref = DBRef('subjects', subject_id)
    else:
        subject_ref = DBRef('subjects', subject['_id'])
    
    
    post_dict = {'body': 'This is an embedded post',
                'author': user_ref,
                'subject': subject_ref
                }
    # post is a document object
    post = db.find_one('posts', post_dict)
    post_ref = None
    if post is None:
        post_dict['created'] = time.time()
        post_id = db.save('posts', post_dict)
        post_ref = DBRef('posts', post_id)
    else:
        post_ref = DBRef('posts', post['_id'])
    
    
    comment_dict = {'body': 'This is an embedded comment',
                    'mode': 0, # comment
                    'author': user_ref,
                    'source': post_ref
                    }
    # comment is a document object
    comment = db.find_one('comments', comment_dict)
    comment_ref = None
    if comment is None:
        comment_dict['created'] = time.time()
        comment_id = db.save('comments', comment_dict)
        comment_ref = DBRef('comments', comment_id)
    else:
        comment_ref = DBRef('comments', comment['_id'])
            
    # Non embedded datas

    new_user = {'username':'test', 
               'password':'test',
               'email':'test@forlocal.com', 
               'role':'0', #0 admin, 1 user
               'phone': '519-334-6720',
               'description':"Local business"
    }
    
    new_subject = {'body': 'This is a test subject'}
    
    new_post = {'body': 'This is a test post',
        'author': user_ref,
        'subject': subject_ref
        }    
    
    new_comment = {'body': 'This is a test comment',
                   'mode': 0,
                   'author': user_ref,
                   'source': post_ref
                   }
    
    comment_reply = {'body': 'This is a test comment reply',
               'mode': 1,
               'author': user_ref,
               'source': comment_ref
               }
    
    
    city = db.find_one('cities',{'city':'London', 'province':'ON'})
    city_ref = DBRef('cities', city['_id'])
    
    address = db.find_one('addresses',{'street_no':'128','street_name':'Lincon St','city': city_ref})
    if address is not None:
        address_ref = DBRef('addresses', address['_id'])
    """
    house = db.find_one('houses', 
            {'name':'my house',
               'description':'my house description',
               'year': '2003',
               'price':'230000', 
               'status':'0', 
               'style':'1', 
               'n_rooms':'3', 
               'n_baths':'3', 
               'n_garages':'2',
               'basement':'1',
               'lots':'40',
               'living_space':'3000',
               'faces':'0', 
               'deck':'1',
               'patio':'0',
               'fence':'1',
               'ac':'1',
               'address':address_ref})
    if house is not None:
        house_ref = DBRef('houses', house['_id'])

    category = db.find_one('categories', {'name':'house'})
    category_ref = DBRef('categories', category['_id'])

    advertisement = db.find_one('advertisements',
                    { 'prefer_contact':0,#0 email, 1 phone
                       'email':'likzhang@gmail.com',
                       'phone': '519-572-0859',
                       'type':0, # 0 rent, 1 sell
                       'category':category_ref,
                       'item':house_ref,
                       'user':user_ref})
    
    if advertisement is not None:
        advertisement_ref = DBRef('advertisements', advertisement['_id'])
    
    
    picture = db.find_one('pictures',{'name':'my house picture',
               'description':'my house outside',
               'fname': r'my.jpg',
               'order':0, # 0 rent, 1 sell
               'category':category_ref,
               'item':house_ref})
    
    if picture is not None:
        picture_ref = DBRef('pictures', picture['_id'])
    """   
    
test_data = TestData() 